// gjw/pages/order/comment.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    w_star:[
      '../images/star.png', '../images/star.png', '../images/star.png', '../images/star.png','../images/star.png',
    ],
    f_star: [
      '../images/star.png', '../images/star.png', '../images/star.png', '../images/star.png', '../images/star.png',
    ],
    hide:true,
    tag:[
      { css:'tag',sign:'物超所值',},
      { css: 'tag', sign:'发货给力',},
      { css: 'tag', sign:'包装精美',},
      { css: 'tag', sign:'捡到宝了',},
      { css: 'tag', sign:'值得信赖'},
      ],
    w_fen:0,
    f_fen:0,
    imgs: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    var tag = that.data.tag
    for (var i = 0; i < tag.length; i++) {
      tag[i].css = 'tag'
    }
		var siteUrl = wx.getStorageSync('siteUrl')
    that.setData({
      info: options,
      siteUrl: siteUrl
    })
  },

  sign:function(e){
    
    var that=this
    var index = e.currentTarget.dataset.index
    var tag = that.data.tag
    // console.log(tag[index].class)
    if (tag[index].css =='tag'){
      tag[index].css ='active'
    }else{
      tag[index].css = 'tag'
    }
    that.setData({
      tag:tag
    })
  },

  // 描述相符度
  w_star:function(e){
    var that = this    
    
    var index = e.currentTarget.dataset.index
    var star=that.data.w_star
    for(let i in star){
      if(i<=index){
        star[i] ="../images/star_on.png"
      }
      else{
        star[i] = "../images/star.png"
      }
    }
    that.setData({
      w_star:star,
      w_fen:index+1
    })
    
  },

  // 发货速度
  f_star: function (e) {
    var that = this
    
    var index = e.currentTarget.dataset.index
    var star = that.data.f_star
    for (let i in star) {
      if (i <= index) {
        star[i] = "../images/star_on.png"
      }
      else {
        star[i] = "../images/star.png"
      }
    }
    that.setData({
      f_star: star,
      f_fen:index+1
    })

  },
  
  place_order:function(e){
    var that = this
		var openid = wx.getStorageSync('openid')
    // var p_id = that.data.info.p_id
    var id = that.data.info.id
    var tag = that.data.tag
    var num = []
    for (let i in tag) {
      if (tag[i].css == "active") {
        num.push(1)
      } else {
        num.push(0)
      }
    }
    var w_fen = that.data.w_fen
    var f_fen = that.data.f_fen
    // if(that.data.info.type=='4'){//为什么要写判断商城。唐问
    //   var imgs = that.data.imgs.join(',')
    //   console.log(imgs)
    //   var cont_txt = that.data.cont_txt
    //   console.log('imgs' + imgs)
    //   console.log('cont_txt' + cont_txt)
    //   app.util.request({
    //     url: 'entry/wxapp/Orderassess',
    //     data: {
    //       openid: openid,
    //       p_id: id,
    //       pricetag: num[0],
    //       speedtag: num[1],
    //       packtag: num[2],
    //       babytag: num[3],
    //       confidetag: num[4],
    //       desstar: w_fen,
    //       speedstar: f_fen,
    //       img: imgs,
    //       content: cont_txt,
    //     },
    //     success: function (res) {
    //       
    //       if (res.data.code == 200) {
    //         wx.showToast({
    //           title: '评论成功',
    //         })
    //         wx.navigateTo({
    //           url: '../order/index',
    //         })
    //       }
    //     }
    //   })
    // }else{
      app.util.request({
        url: 'entry/wxapp/Orderassess',
        data: {
          openid: openid,
          p_id: id,
          pricetag: num[0],
          speedtag: num[1],
          packtag: num[2],
          babytag: num[3],
          confidetag: num[4],
          desstar: w_fen,
          speedstar: f_fen
        },
        success: function (res) {
          
          if (res.data.code == 200) {
            wx.showToast({
							title: '评论成功',
							duration: 2000,
            })
						setTimeout(function () {
							wx.navigateBack({
								delta: 1
							})
						}, 2000)
          }
        }
      })
    // }
  },

  sub:function(e){
    var that=this
    if (that.data.info.type==4){
      var cont_txt = e.detail.value.cont_txt
      if (cont_txt==''){
        wx.showToast({
          title: '请填写评论语',
          icon:'none'
        })
        return
      }else{
        that.uploadimg({
					url: wx.getStorageSync('siteUrl') + 'app/index.php?i=' + wx.getStorageSync('uniacid') + '&c=entry&a=wxapp&do=Uploads&m=zh_gjw',
          path: that.data.imgs
        });
        var imgs = that.data.imgs
        that.setData({
          cont_txt: cont_txt
        })       
        // 商店评论内容和图片
      }
    }
  },


  getImg: function (e) {
    var that = this
    var imgs = that.data.imgs
    wx.chooseImage({
      count: 9 - imgs.length, // 默认9
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        
        var tempFilePaths = res.tempFilePaths
        imgs = imgs.concat(tempFilePaths)
        that.setData({
          imgs: imgs
        })
      }
    })
  },


  

  delete_img: function (e) {
    var that = this
    var imgs = that.data.imgs
    var index = e.currentTarget.dataset.index
    imgs.splice(index, 1)
    that.setData({
      imgs: imgs
    })
  },

  uploadimg: function (data) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'upload_file',
      formData: null,
      success: (resp) => {
        if (resp.data != '') {
          success++;
          data.path[i] = resp.data
          // if (i == data.path.length){
          //   that.place_order(data.path)
          // }
        } else {
          wx.showToast({
            icon: "loading",
            title: "请重试"
          })
        }
      },
      fail: (res) => {
        fail++;
      },
      complete: () => {
        i++;
        if (i == data.path.length) {
          // 图片上传成功之后提交信息
          that.place_order(data.path)
          // that.setData({
          //   imgs: data.path
          // });
        } else {
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }

      }
    });
  },

    /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})